package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureClsAdapter;
import flc.ast.adapter.SearchClsAdapter;
import flc.ast.adapter.SearchRecordAdapter;
import flc.ast.bean.PictureClsBean;
import flc.ast.dataBase.AlbumRepository;
import flc.ast.databinding.ActivitySearchBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;

public class SearchActivity extends BaseAc<ActivitySearchBinding> {

    public static int type;
    private List<PictureClsBean> listClassification = new ArrayList<>();
    private List<PictureClsBean> listShow = new ArrayList<>();
    private SearchRecordAdapter recordAdapter;
    private SearchClsAdapter searchClsAdapter;
    private PictureClsAdapter pictureClsAdapter;
    private String key;

    @Override
    protected int onCreate() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivSearchBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvRecordList.setLayoutManager(layoutManager);
        recordAdapter = new SearchRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);

        FlexboxLayoutManager layoutManager1 = new FlexboxLayoutManager(this);
        layoutManager1.setFlexDirection(FlexDirection.ROW);
        layoutManager1.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvClassificationList.setLayoutManager(layoutManager1);
        searchClsAdapter = new SearchClsAdapter();
        mDataBinding.rvClassificationList.setAdapter(searchClsAdapter);
        searchClsAdapter.setOnItemClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        pictureClsAdapter = new PictureClsAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureClsAdapter);
        pictureClsAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivSearch) {//搜索
            key = mDataBinding.etSearch.getText().toString();
            if (TextUtils.isEmpty(key)) {
                if (type == 1) {
                    ToastUtils.showShort(getString(R.string.please_input_city_name));
                } else if (type == 2) {
                    ToastUtils.showShort(getString(R.string.please_input_label_name));
                }
                return;
            }
            search(key);
            saveRecord();
            getRecord();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof SearchRecordAdapter) {
            key = recordAdapter.getItem(position);
            mDataBinding.etSearch.setText(key);
            search(key);
            saveRecord();
            getRecord();
        } else if (adapter instanceof SearchClsAdapter) {
            key = searchClsAdapter.getItem(position).getTitle();
            mDataBinding.etSearch.setText(key);
            search(key);
            saveRecord();
            getRecord();
        }
    }

    @Override
    protected void initData() {
        getRecord();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getClassification();
        search(key);
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(key);
        List<String> listSp = new ArrayList<>();
        if (type == 1) {
            listSp = AppSpUtils.getLocationSearchList();
        } else if (type == 2) {
            listSp = AppSpUtils.getLabelSearchList();
        }
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(key);
            listSp.add(0, key);
            if (type == 1) {
                AppSpUtils.saveLocationSearchList(listSp);
            } else if (type == 2) {
                AppSpUtils.saveLabelSearchList(listSp);
            }
        } else {
            if (type == 1) {
                AppSpUtils.saveLocationSearchList(listSave);
            } else if (type == 2) {
                AppSpUtils.saveLabelSearchList(listSave);
            }
        }
    }

    private void getRecord() {
        List<String> listSp = new ArrayList<>();
        if (type == 1) {
            listSp = AppSpUtils.getLocationSearchList();
        } else if (type == 2) {
            listSp = AppSpUtils.getLabelSearchList();
        }
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp.size() > 10 ? listSp.subList(0, 10) : listSp);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

    private void getClassification() {
        if (type == 1) {
            listClassification = new AlbumRepository(mContext).getImagesGroupedByCity();
        } else if (type == 2) {
            listClassification = new AlbumRepository(mContext).getImagesGroupedByTag();
        }
        if (listClassification != null && listClassification.size() != 0) {
            searchClsAdapter.setList(listClassification);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
        }
    }

    private void search(String text) {
        if (type == 1) {
            listShow = new AlbumRepository(mContext).getImagesByCity(text);
        } else if (type == 2) {
            listShow = new AlbumRepository(mContext).getImagesByTag(text);
        }
        if (listShow.get(0).getImageList() != null && listShow.get(0).getImageList().size() != 0) {
            pictureClsAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.tvPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
        }
    }

}